package com.zoke.daycarpool.ui;

import org.json.JSONException;
import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

import com.zoke.daycarpool.R;
import com.zoke.daycarpool.base.BaseUi;
import com.zoke.daycarpool.config.CarpoolParamStore;
import com.zoke.daycarpool.config.UrlBank;
import com.zoke.daycarpool.domain.Token;
import com.zoke.daycarpool.domain.UserBeen;
import com.zoke.daycarpool.domain.UserInstense;
import com.zoke.daycarpool.util.LogTest;
import com.zoke.support.http.HttpStores;
import com.zoke.support.http.ZokeErro;
import com.zoke.support.http.ZokeParams;
import com.zoke.support.utils.LogUtil;
import com.zoke.support.view.ZokeToast;

/**
 * 
 * @author jack
 * 
 */
public class AddRoute extends BaseUi {

	private UserBeen mUserBeen;

	private EditText mStartEt;
	private EditText mDestEt;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_addroute);
		// 显示键盘
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);// 显示键盘
		mTitleManager.setTitle("添加路线");
		mEnableSwipeRight = true;
		mTitleManager.setLeftIcon(R.drawable.title_back);
		mUserBeen = UserInstense.getInstense(getApplicationContext()).getUser();
		mStartEt = (EditText) findViewById(R.id.start_ET);
		mDestEt = (EditText) findViewById(R.id.dest_ET);
	}

	/** 提交 **/
	public void onClickComit(View view) {
		fetchAddRote();
	}

	/** 添加路线 **/
	private void fetchAddRote() {
		ZokeParams params = CarpoolParamStore.getDefaultParam(mHashCode);
		String start = mStartEt.getText().toString().trim();
		String dest = mDestEt.getText().toString().trim();
		if (TextUtils.isEmpty(start)) {
			ZokeToast.show(getApplicationContext(), "起点不能为空");
			return;
		}

		if (TextUtils.isEmpty(dest)) {
			ZokeToast.show(getApplicationContext(), "终点不能为空");
			return;
		}

		params.setUrl(UrlBank.doAddRoads);
		params.put("start", start);
		params.put("dest", dest);
		params.put("sid", "" + mUserBeen.getUserId());
		params.put("token", "" + Token.getTokener().getToken());
		HttpStores.getInstense(getApplicationContext()).find(params);
	}

	@Override
	public void onSuccess(ZokeParams out) {
		super.onSuccess(out);
		String result = (String) out.getResult();
		try {
			JSONObject jo = new JSONObject(result);
			int code = jo.optInt("code");
			if (code == 0) {
				Token.getTokener().updateToken(jo.optLong("token"));
				ZokeToast.show(getApplicationContext(), "添加成功");
				AddRoute.this.onBackPressed();
				return;
			}
			ZokeToast.show(getApplicationContext(), "提交失败,请重试");
		} catch (JSONException e) {
			e.printStackTrace();
		}
		LogTest.tag(result);
	}

	@Override
	public void onFails(ZokeParams out) {
		super.onFails(out);
		ZokeToast.show(getApplicationContext(), "提交失败,请重试");
	}

}
